import requests
import json

headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9",
    "content-type": "application/json",
    "origin": "https://www.mianshiya.com",
    "priority": "u=1, i",
    "referer": "https://www.mianshiya.com/",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
}
cookies = {
    "Hm_lvt_8abb85f1b5cfd5f406cdcc6454141898": "1740034256",
    "HMACCOUNT": "9798B0F0E3BE761A",
    "Hm_lvt_c7cedf2eca8990b32ef9f1a0412e7102": "1740034256",
    "Hm_lpvt_c7cedf2eca8990b32ef9f1a0412e7102": "1740034267",
    "Hm_lpvt_8abb85f1b5cfd5f406cdcc6454141898": "1740034267"
}
url = "https://api.mianshiya.com/api/question_bank/list_question"

# 初始化变量
current_page = 1
all_titles = []
question_bank_id = "1860871861809897474"  # 设置 questionBankId 的值

while True:
    data = {
        "current": current_page,
        "pageSize": 20,
        "questionBankId": question_bank_id,
        "tagList": []
    }
    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, cookies=cookies, data=data)

    # 检查响应状态码
    if response.status_code != 200:
        print(f"请求失败，状态码：{response.status_code}")
        break

    # 解析响应数据
    response_data = response.json()

    # 检查是否有数据
    records = response_data.get('data', {}).get('records', [])
    if not records:
        break

    # 提取当前页的 title 并添加到总列表中
    for record in records:
        title = record.get('title')
        if title:
            all_titles.append(title)

    # 增加页码
    current_page += 1

# 将提取的 title 写入文件
file_name = f"{question_bank_id}.txt"
with open(file_name, 'w', encoding='utf-8') as f:
    for title in all_titles:
        f.write(title + '\n')

print(f"数据已成功写入 {file_name} 文件")